import React from 'react';
import {Spin, Card, Button, Table, Popover} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {shipmentTypeHis, partOrderPriceTerms, paymentType, packingTypeHis} from '../Enum';
import routes from './routes';
import {DATETIME_FORMAT, DATE_FORMAT, TABLE, FILES_API} from '../constants';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {getHistoryDetail} from './api';
import {injectIntl} from './intl';

const {Description} = DescriptionList;

class HistoryDetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: [],
        partSaleOrder: {}
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getHistoryDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        partInvoiceDetails: res.data.partInvoiceDetails,
                        partInvoicePackages: res.data.partInvoicePackages
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }

    onCancel = () => {
        this.props.history.push(routes.historyQuery.url());
    }
    render() {
        const {data, partInvoiceDetails, partInvoicePackages} = this.state;
        const {formatMessage} = this.props.intl;
        let totalAmount = 0;
        let totalQuantity = 0;
        if(partInvoiceDetails)
            partInvoiceDetails.forEach(d => {
                totalAmount += (d.totalAmount);
                totalQuantity += (d.quantity);
            });
        const detailColums = [{
            title: formatMessage({
                id: 'historyDetailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.partEngName',
                defaultMessage: '备件英文名称'
            }),
            dataIndex: 'partEngName'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.quantity',
                defaultMessage: '备件数量'
            }),
            dataIndex: 'quantity'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.unitPrice',
                defaultMessage: '单价'
            }),
            dataIndex: 'unitPrice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.totalAmount',
                defaultMessage: '总价'
            }),
            dataIndex: 'totalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.packageCode',
                defaultMessage: '包装编号'
            }),
            dataIndex: 'packageCode'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.perNetWeight',
                defaultMessage: '单位净重'
            }),
            dataIndex: 'perNetWeight'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.totalWeight',
                defaultMessage: '总净重'
            }),
            dataIndex: 'netWeight'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.oldPartCode',
                defaultMessage: '被替代件编号'
            }),
            dataIndex: 'oldPartCode'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.orderCode',
                defaultMessage: '订单编号'
            }),
            dataIndex: 'orderCode'
        }];
        const packageColums = [{
            title: formatMessage({
                id: 'historyDetailPanel.column.packageType',
                defaultMessage: '包装类型'
            }),
            dataIndex: 'packageType',
            render: text => conventEnumValueToString(packingTypeHis, text)
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.packageCode',
                defaultMessage: '包装编号'
            }),
            dataIndex: 'packageCode',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.containerCode',
                defaultMessage: '货柜号'
            }),
            dataIndex: 'containerCode',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.packageLength',
                defaultMessage: '包装长度'
            }),
            dataIndex: 'packageLength',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.packageWidth',
                defaultMessage: '包装宽度'
            }),
            dataIndex: 'packageWidth',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.packageHeight',
                defaultMessage: '包装高度'
            }),
            dataIndex: 'packageHeight',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.packageVolume',
                defaultMessage: '包装体积'
            }),
            dataIndex: 'packageVolume',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.netWeight',
                defaultMessage: '包装净重'
            }),
            dataIndex: 'netWeight',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.grossWeight',
                defaultMessage: '包装毛重'
            }),
            dataIndex: 'grossWeight',
        }, {
            title: this.props.intl.formatMessage({
                id: 'commonDetail.column.action',
                defaultMessage: '操作'
            }),
            dataIndex: 'action',
            render: (text, record) => {
                const {attachments = []} = record;
                if(!attachments.length)
                    return '';
                const fileList = attachments.map(item => (<Description key={item.fileId}><a icon="download" href={`${FILES_API.files}/${item.fileId}`} size="small">
                    {item.fileName}</a></Description>));
                return (<Popover trigger="click" content={<DescriptionList col={1} size="large">
                    {fileList}
                </DescriptionList>}>
                    <a icon="download" size="small">
                        {this.props.intl.formatMessage({
                            id: 'add.downloadFile',
                            defaultMessage: '下载附件'
                        })}</a>
                </Popover>);
            }
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.card.partInvoiceHistory',
                            defaultMessage: '备件预箱单发票历史'
                        })}>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.buyer',
                                defaultMessage: '备件买方'
                            })}>{data.buyer}
                            </Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.code',
                                defaultMessage: '编号'
                            })}>{data.code}</Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.buyerAddress',
                                defaultMessage: '买方地址'
                            })}>{data.buyerAddress}</Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.invoicedDate',
                                defaultMessage: '发票时间'
                            })}>{formatDateTime(data.invoicedDate, DATETIME_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.buyerNumber',
                                defaultMessage: '买方电话'
                            })}>{data.buyerNumber}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.orderCodes',
                                defaultMessage: '订单编号'
                            })}>{data.orderCodes}</Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.buyerFAX',
                                defaultMessage: '买方传真'
                            })}>{data.buyerFAX}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.preReceiptDate',
                                defaultMessage: '预计到港日期'
                            })}>{formatDateTime(data.preReceiptDate, DATE_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.buyerCantact',
                                defaultMessage: '买方联系人'
                            })}>{data.buyerCantact}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.seller',
                                defaultMessage: '备件卖方'
                            })}>{data.seller}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.sellerAddress',
                                defaultMessage: '卖方地址'
                            })}>{data.sellerAddress}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.sellerNumber',
                                defaultMessage: '卖方电话'
                            })}>{data.sellerNumber}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.sellerFAX',
                                defaultMessage: '卖方传真'
                            })}>{data.sellerFAX}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.fromPort',
                                defaultMessage: '发运港口'
                            })}>{data.fromPort}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.destinationPort',
                                defaultMessage: '目的港口'
                            })}>{data.destinationPort}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.paymentHisType',
                                defaultMessage: '付款方式'
                            })}>{conventEnumValueToString(paymentType, data.paymentHisType)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.priceTerms',
                                defaultMessage: '价格条款'
                            })}>{conventEnumValueToString(partOrderPriceTerms, data.priceTerms)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.shipmentType',
                                defaultMessage: '发运方式'
                            })}>{conventEnumValueToString(shipmentTypeHis, data.shipmentType)}</Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.table.details',
                            defaultMessage: '发票清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={detailColums}
                                rowKey="partId"
                                pagination={false}
                                dataSource={partInvoiceDetails}
                                footer={() => <DescriptionList>
                                    <Description term={formatMessage({
                                        id: 'newPanel.operation.totalQuantity',
                                        defaultMessage: '总数量'
                                    })}>{totalQuantity}
                                    </Description>
                                    <Description term={formatMessage({
                                        id: 'newPanel.operation.totalAmount',
                                        defaultMessage: '总金额'
                                    })}>{formatAmount(totalAmount)}
                                    </Description>
                                </DescriptionList>}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.table.packages',
                            defaultMessage: '装箱清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={packageColums}
                                rowKey="packageId"
                                pagination={false}
                                dataSource={partInvoicePackages}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{formatMessage({
                        id: 'historyDetailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

HistoryDetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};

export default injectIntl(HistoryDetailPanel);
